<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备ID" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预警名称" prop="warningName">
        <el-input
          v-model="queryParams.warningName"
          placeholder="请输入预警名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预警等级" prop="warningLevel">
        <el-input
          v-model="queryParams.warningLevel"
          placeholder="请输入预警等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="warningList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="家人姓名" align="center" prop="familyName"/>
<!--      <el-table-column label="设备ID" align="center" prop="deviceId"/>-->

      <el-table-column label="设备类型" align="center" prop="deviceType">
        <template slot-scope="scope">
          <div>{{getTypeName(scope.row.deviceType)}}</div>
        </template>
      </el-table-column>

      <el-table-column label="预警名称" align="center" prop="warningName"/>
      <el-table-column label="预警内容" align="center" prop="warningContent"/>

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <div>{{scope.row.status === '1' ? '待审核' : scope.row.status === '2' ? '待推送' : scope.row.status === '3' ? '已推送' : '忽略'}}</div>
        </template>
      </el-table-column>

      <el-table-column label="随访状态" align="center" prop="followStatus">
        <template slot-scope="scope">
          <div>{{scope.row.followStatus === '0' ? '未随访' : scope.row.followStatus === '1' ? '随访中' : scope.row.followStatus === '2' ? '已完成' : ''}}</div>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate1(scope.row)"
            v-hasPermi="['qiushi:equipment:qiushiwarning:edit']"
          >审核
          </el-button>
          <el-button
            v-if="scope.row.status === '2'"
            size="medium"
            type="primary"
            @click="handleUpdate2(scope.row)"
            v-hasPermi="['qiushi:equipment:qiushiwarning:edit']"
          >推送
          </el-button>
          <el-button
            v-if="scope.row.followStatus === '0'"
            size="medium"
            type="primary"
            @click="handleSend(scope.row)"
            v-hasPermi="['qiushi:equipment:qiushiwarning:handleSend']"
          >随访
          </el-button>
          <el-button
            v-if="scope.row.followStatus === '1'"
            size="medium"
            type="primary"
            @click="handleSend1(scope.row)"
            v-hasPermi="['qiushi:equipment:qiushiwarning:edit']"
          >随访完成
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预警对话框 -->
    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="家人姓名" prop="familyName">
          <div>{{form.familyName}}</div>
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <div>{{getTypeName(form.deviceType)}}</div>
        </el-form-item>
        <el-form-item label="预警名称" prop="warningName">
          <div>{{form.warningName}}</div>
        </el-form-item>
        <el-form-item label="预警内容" prop="warningContent">
          <div>{{form.warningContent}}</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handle2">通 过</el-button>
        <el-button @click="handle4">忽 略</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改预警对话框 -->
    <el-dialog :title="title" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <div>是否推送给{{form.familyName}}?</div>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改预警对话框 -->
    <el-dialog :title="title" :visible.sync="open3" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="问卷" prop="type">
          <el-select v-model="form.formId" placeholder="请选择问题问卷">
            <el-option
              v-for="item in formList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handle5">发 送</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改预警对话框 -->
    <el-dialog :title="title" :visible.sync="open6" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <h1>是否完成{{form.familyName}}的随访?</h1>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handle6">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWarning,
  getWarning,
  delWarning,
  addWarning,
  updateWarning,
  send
} from "@/api/qiushi/equipment/qiushiwarning";
import {listType} from "@/api/qiushi/equipment/qiushidevicetype";
import {listForm} from "@/api/qiushi/followup/qiushiform";

export default {
  name: "Warning",
  data() {
    return {
      // 字典
      status_dict: [{
        value: '1',
        label: '待审核'
      },{
        value: '2',
        label: '通过'
      },{
        value: '4',
        label: '忽略'
      },],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预警表格数据
      warningList: [],
      deviceList: [],
      formList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open1: false,
      open2: false,
      open3: false,
      open4: false,
      open5: false,
      open6: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        familyId: null,
        deviceId: null,
        deviceType: null,
        dataId: null,
        warningName: null,
        warningContent: null,
        warningLevel: null,
        status: null,
        auditTime: null,
        auditUserId: null,
        auditUserName: null,

        familyName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // familyId: [
        //   {required: true, message: "家人ID不能为空", trigger: "blur"}
        // ],
        // deviceId: [
        //   {required: true, message: "设备id不能为空", trigger: "blur"}
        // ],
        // warningName: [
        //   {required: true, message: "预警名称不能为空", trigger: "blur"}
        // ],
        // delFlag: [
        //   {required: true, message: "0正常 1删除不能为空", trigger: "blur"}
        // ],
        // createTime: [
        //   {required: true, message: "创建时间不能为空", trigger: "blur"}
        // ],
        // updateTime: [
        //   {required: true, message: "更新时间不能为空", trigger: "blur"}
        // ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getTypeName(id) {
      for (let i = 0; i < this.deviceList.length; i++) {
        if (this.deviceList[i].id === id) return this.deviceList[i].name
      }
    },
    handle2() {
      this.form.status = '2'
      this.submitForm()
    },
    handle3() {
      this.form.status = '3'
      this.submitForm()
    },
    handle4() {
      this.form.status = '4'
      this.submitForm()
    },
    handle5() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.formName = this.getFormName(this.form.formId)
          if (this.form.id != null) {
            send(this.form).then(response => {
              this.$modal.msgSuccess("发送成功");
              this.open1 = false;
              this.open2 = false;
              this.open3 = false;
              this.getList();
            });
          }
        }
      });
    },
    handle6() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {

            this.form.followStatus = "2";

            updateWarning(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open6 = false;
              this.getList();
            });
          }
        }
      });
    },
    getFormName(id) {
      for (let i = 0; i < this.formList.length; i++) {
        if (this.formList[i].id === id) return this.formList[i].name
      }
    },
    /** 查询预警列表 */
    getList() {
      this.loading = true;

      listType().then(response => {
        this.deviceList = response.rows;
      })

      listForm({}).then(response => {
        this.formList = response.rows;
      });

      listWarning(this.queryParams).then(response => {
        this.warningList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleSend(row) {
      this.reset();
      const id = row.id || this.ids
      getWarning(id).then(response => {
        this.form = response.data;
        this.open3 = true;
        this.title = "发送随访信息";
      });
    },
    handleSend1(row) {
      this.reset();
      const id = row.id || this.ids
      getWarning(id).then(response => {
        this.form = response.data;
        this.open6 = true;
        this.title = "";
      });
    },
    // 取消按钮
    cancel() {
      this.open1 = false;
      this.open2 = false;
      this.open3 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        familyId: null,
        deviceId: null,
        deviceType: null,
        dataId: null,
        warningName: null,
        warningContent: null,
        warningLevel: null,
        status: null,
        auditTime: null,
        auditUserId: null,
        auditUserName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        followStatus: null,

        formId: null,
        formName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open1 = true;
      this.title = "添加预警";
    },
    /** 修改按钮操作 */
    handleUpdate1(row) {
      this.reset();
      const id = row.id || this.ids
      getWarning(id).then(response => {
        this.form = response.data;
        this.open1 = true;
        this.title = "修改预警";
      });
    },
    handleUpdate2(row) {
      this.reset();
      const id = row.id || this.ids
      getWarning(id).then(response => {
        this.form = response.data;
        this.open2 = true;
        this.title = "修改预警";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWarning(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open1 = false;
              this.open2 = false;
              this.getList();
            });
          } else {
            addWarning(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    submitForm1() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.status = '3'

            updateWarning(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open1 = false;
              this.open2 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除预警编号为"' + ids + '"的数据项？').then(function () {
        return delWarning(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('qiushi/equipment/qiushiwarning/export', {
        ...this.queryParams
      }, `warning_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
